<script lang="jsx" setup>
import { computed, nextTick, ref } from 'vue';
import { Button } from 'ant-design-vue';
import dayjs from 'dayjs';
import Table from '@/components/common/table.vue';
import CommonSearch from '@/components/common/common-search.vue';
import Modal from '@/components/common/modal.vue';
import {
  addMoldMaintenance,
  deleteMold,
  editMoldMaintenance,
  exportMoldMaintenance,
  getModelList
} from '@/service/api/modules/workwear/mold-maintenance';
import Add from './modules/add.vue';

const commonSearchRef = ref(null);
const tableRef = ref(null);
const searchParams = ref({
  moldNo: null
});

const searchFormList = [
  {
    type: 'input',
    key: 'moldNo',
    label: '模具编号',
    placeholder: '请输入模具编号',
    span: 24,
    md: 12,
    lg: 8,
    xl: 8,
    xxl: 6
  }
];

const column = [
  {
    key: 'dataIndex',
    dataIndex: 'dataIndex',
    title: '序号',
    width: 70,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'moldNo',
    dataIndex: 'moldNo',
    title: '模具型号',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 150
  },
  {
    key: 'upkeepTypeName',
    dataIndex: 'upkeepTypeName',
    title: '类别',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'moldDesc',
    dataIndex: 'moldDesc',
    title: '问题点描述',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 140
  },
  {
    key: 'replacePart',
    dataIndex: 'replacePart',
    title: '更换配件',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'upkeepDate',
    dataIndex: 'upkeepDate',
    title: '日期',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 130
  },

  {
    key: 'upkeepStartTime',
    dataIndex: 'upkeepStartTime',
    title: '维修时间',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 160,
    customRender: ({ record }) => {
      return dayjs(record.upkeepStartTime).format('YYYY-MM-DD HH:mm:ss');
    }
  },
  {
    key: 'upkeepEndTime',
    dataIndex: 'upkeepEndTime',
    title: '修复时间',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 160,
    customRender: ({ record }) => {
      return dayjs(record.upkeepEndTime).format('YYYY-MM-DD HH:mm:ss');
    }
  },
  {
    key: 'upkeeper',
    dataIndex: 'upkeeper',
    title: '操作人',
    align: 'center',
    checked: true,
    ellipsis: true
  },

  {
    key: 'remark',
    dataIndex: 'remark',
    title: '备注',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'table-cz',
    dataIndex: 'table-cz',
    title: '操作',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 140,
    fixed: 'right',
    customRender: ({ record }) => {
      return (
        <div>
          <Button
            type="link"
            danger
            class="pl-3px pr-3px"
            onClick={() => {
              del(record);
            }}
          >
            删除
          </Button>
          <Button
            type="link"
            class="pl-3px pr-3px"
            onClick={() => {
              handleOpen(record);
            }}
          >
            修改
          </Button>
        </div>
      );
    }
  }
];

function search() {
  tableRef.value.getData(1);
}

function reset() {
  nextTick(() => {
    searchParams.value = {
      keywords: null,
      noticeState: null
    };
    search();
  });
}

const operateType = ref('add');
const modalVisible = ref(false);
const modleAddLoading = ref(false);
const currentRow = ref(null);
const addRef = ref(null);
function handleOpen(row) {
  getMoldNoList();
  currentRow.value = row;
  modalVisible.value = true;
  operateType.value = row ? 'edit' : 'add';
}

function handleClose() {
  modalVisible.value = false;
}
const selcetList = ref([]);
function getMoldNoList() {
  getModelList({
    pageNum: 1,
    pageSize: 9999
  }).then(({ data, error }) => {
    if (!error) {
      selcetList.value = data.list;
    }
  });
}
const recoverRef = ref(null);
async function handleOk() {
  const addObj = {
    ...addRef.value
  };
  await addObj.validateForm();

  modleAddLoading.value = true;
  let pro;
  const params = {
    ...addObj.model
  };

  if (operateType.value === 'add') {
    pro = addMoldMaintenance(params);
  } else {
    params.moldIds = selcetList.value.find(item => item.moldNo === params.moldIds).moldUuid;
    console.log(
      params,
      selcetList.value.find(item => item.moldNo === params.moldIds)
    );
    pro = editMoldMaintenance({
      ...params,
      moldUpkeepUuid: currentRow.value.moldUpkeepUuid
    });
  }
  pro
    .then(({ error }) => {
      if (!error) {
        window.$message.success(`${operateType.value === 'add' ? '新增' : '修改'}成功`);
        handleClose();
        reset();
      }
    })
    .finally(() => (modleAddLoading.value = false));
}

function del(row) {
  window.$modal.confirm({
    title: `确定删除?`,
    onOk: () => {
      deleteMold(row.moldUpkeepUuid).then(({ error }) => {
        if (!error) {
          window.$message.success('删除成功');
          search();
        }
      });
    },
    onCancel: () => {}
  });
}

function exportExcel() {
  exportMoldMaintenance({ ...searchParams.value });
}
</script>

<template>
  <div class="mold-maintenance-page min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <CommonSearch
      ref="commonSearchRef"
      v-model:searchParams="searchParams"
      :form-list="searchFormList"
      :is-search="true"
      @search="search"
      @reset="reset"
    ></CommonSearch>

    <Table
      ref="tableRef"
      v-model:searchParams="searchParams"
      :column="column"
      :show-row-selection="false"
      row-key="moldUpkeepUuid"
      :scroll="{ y: 'auto' }"
      is-init-get-data
      url="/qms/agv/frock/mold/upkeep/record/list"
    >
      <template #tableTitle>
        <AButton type="primary" class="mini-btn mr-8px" size="small" ghost @click="handleOpen(null)">
          <template #icon>
            <icon-ic-round-add class="align-sub text-icon" />
          </template>
          新增
        </AButton>
        <AButton type="primary" size="small" ghost class="mini-btn mr-8px" @click="exportExcel">
          <template #icon>
            <icon-ic-round-download class="align-sub text-icon" />
          </template>
          <span class="ml-3px">导出</span>
        </AButton>
      </template>
    </Table>
    <!-- 新增修改 -->
    <Modal
      v-model:visible="modalVisible"
      :title="`模具维修保养${operateType === 'add' ? '新增' : '修改'}`"
      width="70vw"
      height="500px"
      :centered="true"
      :destroy-on-close="true"
      @close="handleClose"
    >
      <Add ref="addRef" :row-data="currentRow" :operate-type="operateType"></Add>
      <template #footer>
        <div class="modal-footer">
          <AButton :loading="modleAddLoading" type="primary" @click="handleOk">
            {{ operateType === 'add' ? '新增' : '修改' }}
          </AButton>
          <AButton @click="handleClose">关 闭</AButton>
        </div>
      </template>
    </Modal>
  </div>
</template>

<style lang="scss"></style>
